package bhbt.controller.member.wealth;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bhbt.controller.core.BaseController;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.model.log.LogElePay;
import bhbt.model.log.LogMoneyPay;
import bhbt.model.log.LogStorePay;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.log.LogElePayService;
import bhbt.service.log.LogMoneyPayService;
import bhbt.service.log.LogStorePayService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






@Controller
 
public class PayController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogMoneyPayService logMoneyPayService;
	@Resource
	private LogElePayService logElePayService;
	@Resource
	private LogStorePayService logStorePayService;
	@Resource
	private ParamWageService paramWageService;

	@RequestMapping("member/wealth/payShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		ParamWage paramWage1013=paramWageService.getByKey(1013);
		ParamWage paramWage1014=paramWageService.getByKey(1014);
		
		request.setAttribute("paramWage1013",paramWage1013.getValue());
		request.setAttribute("paramWage1014",paramWage1014.getValue());
		
		return "member/wealth/pay";
	}

	 
	
	@RequestMapping("member/wealth/ajaxGetPersonByloginName.shtml")
	@ResponseBody
	public void ajaxTuRecommendAction( HttpServletRequest request, HttpServletResponse response) {
		
		 
		Map formMap = getParameterMap(request); 
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);
		 
		HashMap resultformMap = new HashMap();
		if(member==null){
			
			renderSuccess(response, false, resultformMap);
		}else{
			 
			resultformMap.put("personName", member.getPersonName());
			 
			renderSuccess(response, true, resultformMap); 
			
		}
		
	}

	
	
	@RequestMapping("member/wealth/pay.shtml")
	public String pay(HttpServletRequest request, ModelMap model) throws Exception{
		
		Boolean result=true;
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		 
		ParamWage paramWage1013=paramWageService.getByKey(1013);
		ParamWage paramWage1014=paramWageService.getByKey(1014);
		
		Map formMap = getParameterMap(request);
		String twoPassword = MapUtil.getString(formMap, "twoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
   	 
    	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"安全密码不正确。");
    		result=false;
    	}else{
    		
    		//接收会员验证
    		String loginName = MapUtil.getString(formMap, "loginName");
        	Member toMember = memberService.executMemberByLoginName(loginName);
        	if(toMember==null||toMember.getAccountActive().equals("F")){
        		setMessage(model,"会员编号不存在或者未激活。");
        		result=false;
        	}else{
        		//转账金额验证
        		Double num = MapUtil.getDoubleValue(formMap, "num");
        		if(num<paramWage1013.getValue()){
        			setMessage(model,"转账金额必须大于："+paramWage1013.getValue());
            		result=false;
        		}else{
        			if((num%paramWage1014.getValue())!=0){
        				setMessage(model,"转账金额必须是："+paramWage1014.getValue()+"的整数。");
                		result=false;
    				} else{
    					//转账类型
    					int payType = MapUtil.getIntValue(formMap, "payType");
    					String content = MapUtil.getString(formMap, "content");
    					 
    					//转账
    					if(payType==10){
    						if(member.getMoney()<num){
    							setMessage(model,"现金币余额不足");
    	                		result=false;
    						}else{
    							LogMoneyPay logMoneyPay = new LogMoneyPay();
    							logMoneyPay.setFromMemberId(member.getId());
    							logMoneyPay.setFromYu(member.getMoney());
    							logMoneyPay.setToMemberId(toMember.getId());
    							logMoneyPay.setToYu(toMember.getMoney());
    							logMoneyPay.setNum(num);
    							logMoneyPay.setContent(content);
    							logMoneyPay.setContent(MapUtil.getString(formMap, "content"));
    							logMoneyPayService.savePay(logMoneyPay);
    							return "redirect:logMoneyList.shtml";
    							
    						}
    					}
    					  
    				}
        		}
        	}
    		
    		
    		
    		
    		
    		 
    	}
		
    	if(!result){
    		
    		request.setAttribute("member",member);
    		request.setAttribute("paramWage1013",paramWage1013.getValue());
    		request.setAttribute("paramWage1014",paramWage1014.getValue());
    		
    		return "member/wealth/pay";
    	}
		
		return "redirect:logMoneyList.shtml";
	}
}
